package com.ls.lsqc.modules.sys.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ls.lsqc.modules.sys.utils.SystemMessageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ls.lsqc.common.persistence.AjaxDone;
import com.ls.lsqc.common.persistence.BaseEntity;
import com.ls.lsqc.common.utils.ResourcesUtil;
import com.ls.lsqc.common.utils.StringUtils;
import com.ls.lsqc.common.web.BaseController;
import com.ls.lsqc.modules.sys.entity.Dict;
import com.ls.lsqc.modules.sys.entity.Shop;
import com.ls.lsqc.modules.sys.service.ShopService;
import com.ls.lsqc.modules.sys.utils.DictUtils;
import com.ls.lsqc.modules.sys.utils.UserUtils;


@Controller
@RequestMapping(value = "${adminPath}/sys/myshop")
public class MyShopController extends BaseController{

    @Autowired
    private ShopService shopService;
    
    
    /**
     * 取店铺信息
     * @param id
     * @return
     */
    @ModelAttribute
    public Shop get(@RequestParam(required=false) String id) {
        
        if (StringUtils.isNotBlank(id)){
            Shop shop=shopService.getShop(Long.parseLong(id));
            return shop;
        }else{
            return new Shop();
        }
    }
    
    
    /**
     * 定制项目列表(初始list列表显示)
     * @param model
     * @return
     */
    @RequiresPermissions("sys:myshop:view")
    @RequestMapping(value = {"list", ""})
    public String list(Shop shop, Model model) {
        
        shop =new Shop();
      
        Long Shopid=UserUtils.getUser().getShop().getId();
        
        shop=shopService.getShop(Shopid);
        // 店铺选中款式
        List<Map<String,Object>> shopStyleSelectList=shopService.findShopStyle(shop.getId().toString());
        
        model.addAttribute("shopStyleSelectList", shopStyleSelectList);

        model.addAttribute("shop", shop);
        
        return "modules/sys/myShop";
    }
    
    /***
     * 店铺编辑查看页
     * @param shop
     * @param request
     * @param model
     * @return
     */
    @RequiresPermissions("sys:myshop:edit")
    @RequestMapping(value = "form")
    public String form(Shop shop,HttpServletRequest request, Model model) {
        
        // 店铺选中款式
        List<Map<String,Object>> shopStyleSelectList=shopService.findShopStyle(shop.getId().toString());
        
        model.addAttribute("shopStyleSelectList", shopStyleSelectList);
        
        model.addAttribute("shop", shop);
        
        return "modules/sys/myShopEdit";
        
    }
    
    
    /**
     * 联动返回城市
     * @param code
     * @param model
     * @return
     */
    @RequiresPermissions("sys:myshop:view")
    @RequestMapping(value = "city")
    @ResponseBody
    public String city(String code,Model model,HttpServletResponse response ) {
        
        List<Dict> cityList=DictUtils.getDictList(code);
        
        return renderString(response, cityList);
       
    }
    
    /**
     * 保存店铺信息
     * @param shop
     * @param request
     * @param model
     * @param response
     * @return
     */
    @RequiresPermissions("sys:myshop:edit")
    @RequestMapping(value = "save")
    @ResponseBody
    public String save(Shop shop, HttpServletRequest request, Model model, HttpServletResponse response ) {
        
        if(shop.getId()!=null){
            //判断数据是否删除
            Shop shopNew=get(shop.getId().toString());
            if(shopNew==null || BaseEntity.DEL_FLAG_DELETE.equals(shopNew.getDeleteFlag())){
                return renderString(response,AjaxDone.error(getText("This_record_has_been_deleted_or_modified_the_operation_failed")));
            }
            //排他
            if(shop.getVersion()==get(shop.getId().toString()).getVersion()){
                shop.setVersion(shop.getVersion()+1);
            }else{
                return renderString(response,AjaxDone.error(getText("This_record_has_been_deleted_or_modified_the_operation_failed")));
            }
        }
        
        try {
         // 保存店铺信息
            shopService.saveShopOrUser(shop);
            return renderString(response,AjaxDone.ok(SystemMessageUtils.getMessageName( "shop_userSaveShop")));
   
         } catch (Exception e) {
             // 记录日志
             logger.error(e.getMessage(), e);
            return renderString(response,AjaxDone.error(getText("System_error_please_contact_your_administrator")))  ;
         }
     
    }
    
    /**
     * 验证店铺名是否有效
     * 
     * @param oldLoginName
     * @param loginName
     * @return
     */
    @ResponseBody
    @RequiresPermissions("sys:myshop:edit")
    @RequestMapping(value = "checkShopName")
    public String checkShopName(String oldShopName, String shopName) {
        if (shopName != null && shopName.equals(oldShopName)) {
            return "true";
        } else if (shopName != null&& shopService.getShopByShopName(shopName) == null) {
            return "true";
        }
        return "false";
    }
    
}
